package com.itboye.textileexpres.top;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.R.color;
import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.alipay.a.a.a;
import com.itboye.textileexpes.beans.Category_TypeTwo;
import com.itboye.textileexpes.beans.ForresterBeans;
import com.itboye.textileexpes.beans.NameTopFragmentBen;
import com.itboye.textileexpes.presenter.MainCategoryPresenter;
import com.itboye.textileexpes.presenter.MainCategoryTypeTwoPresenter;
import com.itboye.textileexpes.repository.MainCategoryRepositoryTypeTwo;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseFragment;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.top.MyExpandableListAdapter.ViewHolder;
import com.itboye.textillexepres.db.PropSub;
import com.itboye.textillexepres.db.SearchDBHelper;

public class MeOfFragmentGoodsSearch extends BaseFragment implements
		OnClickListener, Observer {

	ExpandableListView mListView;
	MyExpandableListAdapter adapter;
	String fid;
	int cateID;
	MainCategoryPresenter presenter;
	protected List<NameTopFragmentBen> arrayListForm = new ArrayList<NameTopFragmentBen>();
	int type;
	MainCategoryTypeTwoPresenter twopresenter;
	String proID;

	public MeOfFragmentGoodsSearch(int cateID, int type) {
		// TODO Auto-generated constructor stub
		this.cateID = cateID;
		this.type = type;
	}

	public MeOfFragmentGoodsSearch() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_top_me;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		presenter = new MainCategoryPresenter(this);
		twopresenter = new MainCategoryTypeTwoPresenter(this);
		twopresenter.queryCate(cateID);
		mListView.setGroupIndicator(null);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		}

	}

	@SuppressLint("ResourceAsColor")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		final ResultEntity entity = handlerError(data);

		if (entity != null) {

			if (entity.getEventType() == MainCategoryTypeTwoPresenter.Category2_success) {
				// arrayList.addAll((List<Category_TypeTwo>) entity.getData());
				arrayListForm = (List<NameTopFragmentBen>) entity.getData();
				adapter = new MyExpandableListAdapter(getActivity(),
						arrayListForm);
				mListView.setAdapter(adapter);
				mListView.setOnChildClickListener(new OnChildClickListener() {

					@Override
					public boolean onChildClick(ExpandableListView parent,
							View v, int groupPosition, int childPosition,
							long id) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(getActivity(),
								ActivityTypeDetailTwo.class);
						intent.putExtra("proid", proID);
						intent.putExtra("from", "has_prop");
						intent.putExtra("name",
								arrayListForm.get(groupPosition).getChildren()
										.get(childPosition).getName());
						intent.putExtra("cateid",
								arrayListForm.get(groupPosition).getChildren()
										.get(childPosition).getId());
						startActivity(intent);
						return false;
					}
				});
			}
		}
	}

	public class MyExpandableListAdapter implements ExpandableListAdapter {
		Context context;
		private List<NameTopFragmentBen> armTypes;;

		public MyExpandableListAdapter(Context context,
				List<NameTopFragmentBen> armTypes) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.armTypes = armTypes;
		}

		public MyExpandableListAdapter(Context context,
				List<Category_TypeTwo> armTypes, OnClickListener totopclicker) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.armTypes = this.armTypes;
		}

		@Override
		public void registerDataSetObserver(DataSetObserver observer) {

		}

		@Override
		public void unregisterDataSetObserver(DataSetObserver observer) {

		}

		@Override
		public int getGroupCount() {
			return armTypes.size();
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return armTypes.get(groupPosition).getChildren().size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return armTypes.get(groupPosition);
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return armTypes.get(groupPosition).getChildren().get(childPosition);
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater inflater = LayoutInflater.from(context);
				convertView = (LinearLayout) inflater.inflate(
						R.layout.item_goodsfragmenta, null);
				holder.type_tv = (TextView) convertView
						.findViewById(R.id.type_tv);
				holder.to_top = (ImageView) convertView
						.findViewById(R.id.to_top);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (isExpanded) {
				holder.to_top.setBackgroundResource(R.drawable.more_unfold);
			} else {
				holder.to_top.setBackgroundResource(R.drawable.less);
			}
			holder.type_tv.setText(armTypes.get(groupPosition).getName());
			holder.to_top.setTag(groupPosition);
			return convertView;

			// return getTextView(armTypes.get(groupPosition).getName());

		}

		class ViewHolder {
			TextView type_tv;
			ImageView to_top;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater inflater = LayoutInflater.from(context);
				convertView = (LinearLayout) inflater.inflate(
						R.layout.item_goodsfragmenta_dapter, null);
				holder.type_tv = (TextView) convertView
						.findViewById(R.id.type_tv);
				holder.to_top = (ImageView) convertView
						.findViewById(R.id.to_top);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
//			holder.to_top.setBackgroundResource(R.drawable.nmae_add);
			holder.type_tv.setText(armTypes.get(groupPosition).getChildren()
					.get(childPosition).getName());
			return convertView;

		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		@Override
		public boolean areAllItemsEnabled() {
			return false;
		}

		@Override
		public boolean isEmpty() {
			return false;
		}

		@Override
		public void onGroupExpanded(int groupPosition) {

		}

		@Override
		public void onGroupCollapsed(int groupPosition) {

		}

		@Override
		public long getCombinedChildId(long groupId, long childId) {
			return 0;
		}

		@Override
		public long getCombinedGroupId(long groupId) {
			return 0;
		}

	}
}
